/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package subterfuge.dialog;

import emergence.game.level.Level;
import emergence.game.level.Level.LevelResize;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author Nathan Templon
 */
public class ExpandLevelDialog extends JDialog implements ActionListener {

    private final String[] columnNames = {
        "Direction",
        "Current Size",
        "Tiles to Add",
        "Position of New Tiles"
    };
    
    private final String[] widthOptions = {
        "Left",
        "Right"
    };
    private final String[] heightOptions = {
        "Top",
        "Bottom"
    };
    
    private final Dimension preferredSize = new Dimension(500, 200);

    private final Level level;

    private JScrollPane tableScroll;
    private JTable table;
    private Object[][] values;
    private final JComboBox widthCombo = new JComboBox(widthOptions);
    private final JComboBox heightCombo = new JComboBox(heightOptions);
    
    private final TableCellEditor widthEditor = new DefaultCellEditor(widthCombo);
    private final TableCellEditor heightEditor = new DefaultCellEditor(heightCombo);
    
    private JPanel choicesPanel;
    private JButton okButton;
    private JButton cancelButton;
    
    private LevelResize resize = null;

    public ExpandLevelDialog(JFrame parent, Level level) {
        super(parent, "Expand Level Dialog", true);

        this.level = level;

        loadInitialValues();
        buildGui();
    }
    
    private void loadInitialValues() {
        values = new Object[2][columnNames.length];
        values[0][0] = "Width";
        values[0][1] = level.width();
        values[0][2] = 0;
        values[0][3] = widthOptions[1];
        values[1][0] = "Height";
        values[1][1] = level.height();
        values[1][2] = 0;
        values[1][3] = heightOptions[1];
    }

    private void buildGui() {
        // Instantiate the table
        // Overide the getCellEditor method to allow for different editors for the width and height editing.
        table = new JTable(new ExpandLevelTableModel()) {
            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                if (column != 3) {
                    return super.getCellEditor(row, column);
                }
                else {
                    if (row == 0) {
                        return widthEditor;
                    }
                    else if (row == 1) {
                        return heightEditor;
                    }
                }
                return super.getCellEditor(row, column);
            }
            
            @Override
            public Dimension getPreferredScrollableViewportSize() {
                return getPreferredSize();
            }
        };
        tableScroll = new JScrollPane(table);
        tableScroll.setBorder(BorderFactory.createEmptyBorder(10, 10, 25, 10)); // top, left, bottom, right
        
        okButton = new JButton("Apply");
        okButton.addActionListener(this);
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        choicesPanel = new JPanel();
        choicesPanel.setLayout(new BorderLayout());
        choicesPanel.add(okButton, BorderLayout.LINE_START);
        choicesPanel.add(Box.createRigidArea(new Dimension(50, 10)));
        choicesPanel.add(cancelButton, BorderLayout.LINE_END);
        choicesPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 10));
        
        this.setLayout(new BorderLayout());
        this.add(tableScroll, BorderLayout.PAGE_START);
        this.add(choicesPanel, BorderLayout.PAGE_END);
        
        this.pack();
    }

    public LevelResize getResize() {
        return resize;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        
        if (source == okButton) {
            resize = new LevelResize();
            
            String leftRight = (String)values[0][3];
            if (leftRight.equals(widthOptions[0])) {
                // This is Left
                resize.setAddLeft((Integer)values[0][2]);
            }
            else if (leftRight.equals(widthOptions[1])) {
                // This is Right
                resize.setAddRight((Integer)values[0][2]);
            }
            
            String topBot = (String)values[1][3];
            if (topBot.equals(heightOptions[0])) {
                // This is Top
                resize.setAddTop((Integer)values[1][2]);
            }
            else if (topBot.equals(heightOptions[1])) {
                // This is Bot
                resize.setAddBot((Integer)values[1][2]);
            }
            this.dispose();
        }
        else if (source == cancelButton) {
            this.dispose();
        }
    }

    private class ExpandLevelTableModel extends AbstractTableModel {

        @Override
        public int getRowCount() {
            return values.length;
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            return values[rowIndex][columnIndex];
        }
        
        @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }
        
        @Override
        public boolean isCellEditable(int row, int col) {
            switch(col) {
                case 0:
                    return false;
                case 1:
                    return false;
                case 2:
                    return true;
                case 3:
                    return true;
                default:
                    return false;
            }
        }
        
        @Override
        public void setValueAt(Object value, int row, int col) {
            values[row][col] = value;
        }
        
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

    }

}
